<?php
/**
 * @authors liuqinglin 
 * @email (2961181429@qq.com) 
 * @(https://my.oschina.net/shp32131)
 * @date  2016-12-07 11:01:07
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		
	}
	// 加载登录页面
	public function index()
	{
		$this->load->view('login');
	}
	//生成验证码
	public function captcha()
	{
		$this->load->helper('captcha');
		$vals = array(
			    	'img_width'  => '200', 
			    	'img_height' => 23
		);
		$this->session->code = strtolower(create_captcha($vals));
	}
	//登录控制
	public function signin()
	{
		if($this->session->code === strtolower($this->input->post('code',TRUE)))
		{
			$user = $this->input->post(array('name','password'),TRUE);
			$this->load->model('admin_model');
			if($this->admin_model->get_one('name',$user['name']) && $this->admin_model->get_one('password',md5($user['password'])))
			{
				$this->session->userinfo = $this->admin_model->get_one('name',$user['name']);
				redirect('admin/admin');
			}
			else
			{
				$data['message'] = '用户名或密码不正确！';
				$this->load->view('login',$data);			
			}
		}
		else
		{	
			$data['message'] = '验证码输入不正确！';
			$this->load->view('login',$data);
		}
	}
}
